package xuedou.skymoni.dao;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public class ConnectionManager {
	private static BasicDataSource dataSource = null;  
	  
    public ConnectionManager() {  
    }  
  
    public static void init() {  
  
        if (dataSource != null) {  
            try {  
                dataSource.close();  
            } catch (Exception e) {  
                //  
            }  
            dataSource = null;  
        }  
  
        try {  
            Properties p = new Properties();  
            p.setProperty("driverClassName", "com.mysql.jdbc.Driver");  
            p.setProperty("url", "jdbc:mysql://192.168.218.66:3306/skybjmn?characterEncoding=UTF-8&amp;useUnicode=true");  
            p.setProperty("password", "kjgw232xxMx");  
            p.setProperty("username", "skymn");  
            p.setProperty("maxActive", "20");  
            p.setProperty("maxIdle", "20");  
            p.setProperty("minIdle", "4");
            p.setProperty("initialSize", "3");
            p.setProperty("maxWait", "6000");  
            p.setProperty("removeAbandoned", "false");  
            p.setProperty("removeAbandonedTimeout", "180");  
            p.setProperty("testOnBorrow", "false");  
            p.setProperty("logAbandoned", "false"); 
            p.setProperty("validationQuery", "select 1");
            p.setProperty("testWhileIdle", "true");
            p.setProperty("testOnReturn", "false");
            p.setProperty("validationQueryTimeout", "3");
            p.setProperty("timeBetweenEvictionRunsMillis", "30000");
            p.setProperty("numTestsPerEvictionRun", "20");
            dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);  
  
        } catch (Exception e) {  
            //  
        }  
    }  
  
  
    public static synchronized Connection getConnection() throws  SQLException {  
        if (dataSource == null) {  
            init();  
        }  
        Connection conn = null;  
        if (dataSource != null) {  
            conn = dataSource.getConnection();  
        }  
        return conn;  
    }  
    public static void main(String args[]) throws SQLException
    {
    	Connection conn=ConnectionManager.getConnection();
//    	if(conn.get)
    	PreparedStatement pstmt=conn.prepareStatement("select count(*) from edu_course;");
		ResultSet rs=pstmt.executeQuery();
		while(rs.next())
		{
			System.out.println(rs.getInt(1));
		}
		if(rs != null){
            try { rs.close();} catch (SQLException e) {e.printStackTrace();}
        }
        if(pstmt != null){
            try { pstmt.close();} catch (SQLException e) {e.printStackTrace();}
        }
        if(conn != null){
            try { conn.close();} catch (SQLException e) {e.printStackTrace();}
        }
    }
}
